
/* -----------------
* brief 
* 1. this is code gen by tools
*/

package service

import (
	"errors"
	"github.com/chanxuehong/wechat/mchv3/marketing/busifavor"
	"github.com/mjiulee/lego"
	"saas_sys_base/common"
)
import . "saas_sys_base/modules/coupon/models"

type MchCouponService struct {
	// add your model here
	tbMchCoupon MchCoupon
}


func (t* MchCouponService)ListMchCoupon(keyword string,page,psize int) ([]map[string]string, int64, error) {
	// 业务查询
	queryMap := map[string]interface{}{
		"table":  map[string]string{"table": "tb_mch_coupon", "alias": "t1"},
		"fields": []string{"t1.*"},
		"join": []map[string]string{
			// map[string]string{"table": "tb_xx as t2", "on": "t2.xid = t1.id"},
		},
		"where": []lego.QMapWhereField{
			//{"t1.if_del", "=", 0, "AND", false},
		},
		"search": map[string]string{
			//"t1.id": req.Keyword,
		},
		"orders": []string{"t1.id asc"},
		"page": map[string]int{
			"page":  page,
			"psize": psize,
		},
	}
	return lego.QueryListByMap(queryMap)
}

/* 根据id获取记录
 * -----------------
 */
func (t* MchCouponService) GetMchCouponById(id int64) *MchCoupon {
	return t.tbMchCoupon.RecodeById(id)
}

/* 添加
 * -----------------
 */
func (t* MchCouponService) SaveMchCoupon(item2add *MchCoupon) bool {
	return t.tbMchCoupon.AddRecode(item2add)
}

/* 删除(硬删除)*/
func (t* MchCouponService) DelMchCouponById(id int64) bool {
	return t.tbMchCoupon.DelRecodeById(id)
}

/* 更新
 * -----------------*/
func (t* MchCouponService) UpdateMchCoupon(rc *MchCoupon) bool {
	return t.tbMchCoupon.UpdateRecode(rc)
}


//以下为自定义函数
func (t *MchCouponService) CreateCouponByWho(who *common.Who, couponPara *busifavor.MchCoupon, StockIdInfo *busifavor.CreateBusiFavorStockResp, CouponMode common.COUPON_MODE) (*MchCoupon, error) {

	item2add := NewMchCoupon(who, couponPara, CouponMode)
	item2add.Status = common.STATE_YES
	item2add.CreateBusiFavorStockResp = StockIdInfo
	isSuccess := t.tbMchCoupon.AddRecode(item2add)
	if isSuccess != true {
		return nil, errors.New("t.tbMchCoupon.AddRecode failed")
	}
	return item2add, nil
}


func (t* MchCouponService) GetMchCouponByStockId(stockId string) *MchCoupon {
	return t.tbMchCoupon.RecodeByStockId(stockId)
}